﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AO.Core.Config;
using System.IO;

namespace ChatEngine
{
    internal struct ChatLog
    {
        public DateTime CreationDate {get;set;}
        public StreamWriter Stream { get; set; }
    }

    public static class ChatLogger
    {
        static bool m_bEnabled;

        /// <summary>
        /// Handles to log files for various channels.
        /// </summary>
        static Dictionary<string, ChatLog> m_LogFiles = new Dictionary<string, ChatLog>();

        static ChatLogger()
        {
            if (ConfigReadWrite.Instance.CurrentConfig.LogChat)
            {
                m_bEnabled = true;
            }
        }

        private static string GetLogString(string Channel, DateTime Time)
        {
            StringBuilder Sb = new StringBuilder();
            Sb.Append(@"Logs\");
            Sb.Append(Channel);
            Sb.Append("-");
            Sb.Append(Time.Year.ToString());
            Sb.Append("-");
            Sb.Append(Time.Month.ToString());
            Sb.Append("-");
            Sb.Append(Time.Day.ToString());
            Sb.Append(".log");

            if (!Directory.Exists("ChatLogs"))
            {
                try
                {
                    Directory.CreateDirectory("ChatLogs");
                }
                catch { }
            }

            return Sb.ToString();
        }

        public static void WriteString(string Channel, string Data, string sender)
        {
            //Make sure the config has chat logging enabled.
            if (!m_bEnabled)
                return;
            /*
            //Check if this is the first entry for this channel.
            if (!m_LogFiles.ContainsKey(Channel))
            {
                LoadLog(Channel);
            }

            //Check if it's a new day and we must close current log
            //for channel, and open a new one.
            if (CheckLogRefresh(Channel))
            {
                LoadLog(Channel);
            }
            */
            LoadLog(Channel);
            ChatLog LogFile = m_LogFiles[Channel];
            DateTime Timestamp = DateTime.Now;
            StringBuilder LogEntry = new StringBuilder();
            LogEntry.Append("[");
            if (Timestamp.Hour < 10)
                LogEntry.Append("0");
            LogEntry.Append(Timestamp.Hour.ToString());
            LogEntry.Append(":");
            if (Timestamp.Minute < 10)
                LogEntry.Append("0");
            LogEntry.Append(Timestamp.Minute.ToString());
            LogEntry.Append("] ");
            LogEntry.Append(sender);
            LogEntry.Append(": ");
            LogEntry.Append(Data);

            LogFile.Stream.WriteLine(LogEntry.ToString());
            LogFile.Stream.Close();
        }

        private static bool CheckLogRefresh(string Channel)
        {
            if (DateTime.Now.Day != m_LogFiles[Channel].CreationDate.Day)
                return true;
            return false;
        }

        private static void LoadLog(string Channel)
        {

            if (m_LogFiles.ContainsKey(Channel))
                m_LogFiles[Channel].Stream.Close();
            
            DateTime NewLogDate = DateTime.Now;
            ChatLog NewLog = new ChatLog();
            NewLog.CreationDate = NewLogDate;
            NewLog.Stream = null;
            string sLogFilename = GetLogString(Channel, NewLogDate);
            NewLog.Stream = new StreamWriter(sLogFilename, true);
            m_LogFiles[Channel] = NewLog;
        }
    }
}
